<?php
 /*
  *商家后台订单模型类
  */
namespace Home\Model; 
use Think\Model;
class OrderModel extends Model
{
	/*
	 * 订单操作记录
	 * @param orderid
	 * return array
	 */
	public function orderLog($orderid=0)
	{
		$orderid=!empty($orderid)?$orderid:0;
		if(empty($orderid)){
			return '';
		}
		$orderAction=M('order_action');
		$logs=$orderAction->where('order_id=%d',$orderid)

		                  ->getField('actionid,order_id,action_user,(order_status+shipping_status+pay_status) as status ,action_note,FROM_UNIXTIME(edittime, "%Y-%m-%d %H:%i:%S") as edittime');
		                  
		if (!empty($logs)) {
			return $logs;
		}else{
			return '';
		}
	}

	/*
 	 *订单列表
 	 *@param page
 	 *@author blakefan 
 	 * return json
 	 */
 	public function index($arguments)
 	{
 		$shopid=$_SESSION['shopid'];
 		$arg['starttime']=$arguments['starttime'];
 		$arg['endtime']=$arguments['endtime'];
 		$arg['orderarea']=empty($arguments['orderarea'])?99:$arguments['orderarea']; //默认网购区
 		$page=$arguments['page'];
 		$condition=' 1=1 ';
 		if(!empty($shopid))
 		{
 			$condition.=" AND shopid=".$shopid;
 		}
 		if(!empty($arg['starttime']) && !empty($arg['endtime']))
 		{
 			$condition.=' AND a.create_time BETWEEN '.$arg['starttime'] .' AND '.$arg['endtime'] ;
 		}
 		if($arg['orderarea'] !=99)
 		{
 			$condition.=' AND a.areatype='.$arg['orderarea'];
 		}

 		$count=M('order a')->join("inner join cart_order_item b on a.orderid=b.orderid")
 		->where($condition)->count();
 		
 		$pagearr= getPageArr($page,C('PAGESIZE'),$count);

 		$result=M('order a')->join("inner join cart_order_item b on a.orderid=b.orderid")
 		->where($condition)
 		->order('a.orderid desc')
 		->limit($pagearr['start'],$pagearr['pagesize'])
 		->getField('a.orderid,a.order_sn,a.order_status+a.shipping_status+a.pay_status as allstatus, a.create_time,a.pay_type,a.order_amount,b.itemname,b.specvale,b.number,b.price');
 		
 		foreach($result as $k=>&$v)
 		{
 			$v['create_time']=date("Y-m-d",$v['create_time']);
 			$v['pay_type']=($v['pay_type']==false)?'线上支付':'线下支付';

 			switch ($v['allstatus'])
 			{
 				case '0':
 				$v['orderstatus']='未确认';
 				break;
 				case '1':
 				
 				$v['orderstatus']='未付款';
 				break;
 				case '3':
 				$v['orderstatus']='已付款';
 				break;
 				case '6':
 				
 				$v['orderstatus']='配货中';
 				break;
 				case '8':
 				
 				$v['orderstatus']='发货中';
 				break;
 				case '9':
 				$v['orderstatus']='已收货';
 				break;
 				case '4':
 				$v['orderstatus']='退货';
 				break;
 				case '2':
 				$v['orderstatus']='取消';
 				break;
 			}
 		}
 		$temp['pagearr']=$pagearr;
 		$temp['orders']=$result;
 		return $temp;
 	}
}
